package com.yykt.weshare.controller;

import com.github.pagehelper.PageInfo;
import com.yykt.weshare.entity.Users;
import com.yykt.weshare.service.UsersService;
import com.yykt.weshare.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    UsersService usersService;

    @PostMapping("/login")
    public JsonResult login(@RequestParam String username, @RequestParam String password, HttpServletRequest request) {
        Users user = usersService.checkLogin(username, password);
        if (user != null) {
            HttpSession session = request.getSession();
            session.invalidate();
            session = request.getSession();
            session.setAttribute("user", user);
            return JsonResult.success("登陆成功");
        }
        return JsonResult.success("登陆失败");
    }

    @GetMapping("/demo")
    public JsonResult demo(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        if (user == null) {
            return JsonResult.error("没有登陆");
        } else {
            return JsonResult.success(user);
        }
    }

    @PostMapping("/logout")
    public JsonResult logout(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            session.invalidate();
        }
        return JsonResult.success("退出成功");
    }

    @GetMapping("/user/list")
    public JsonResult userList(
            @RequestParam(required = false, defaultValue = "") String username,
            @RequestParam(required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "2") int pageSize
    ) {
        PageInfo<Users> all = usersService.findALL(username, pageNum, pageSize);
        return JsonResult.success(all);
    }

    @PostMapping("/user/offline")
    public JsonResult userOffline() {
        return JsonResult.success();
    }

    @PostMapping("/user/online")
    public JsonResult userOnline() {
        return JsonResult.success();
    }

    @PostMapping("/user/review")
    public JsonResult userReview() {
        return JsonResult.success();
    }

    @GetMapping("/item/list")
    public JsonResult itemList() {
        return JsonResult.success();
    }

    @PostMapping("/item/review")
    public JsonResult itemReview() {
        return JsonResult.success();
    }

    @GetMapping("/comment/list")
    public JsonResult commentList() {
        return JsonResult.success();
    }

    @PostMapping("/comment/review")
    public JsonResult commentReview() {
        return JsonResult.success();
    }

    @GetMapping("/item/completed/list")
    public JsonResult completedList() {
        return JsonResult.success();
    }
}
